

<?php
	class UserList extends CI_Controller{
		
		function __construct()
		{
			 
			parent::__construct();
			$this->load->library('table');
			 
		}
		
		function index()
		{
 
		}
		
//		// Admin Login
//		function login(){
//			// 验证登陆信息
//			$username = $_REQUEST['username'];
//			$password = $_REQUEST['password'];
//	
//			//登陆错误,跳转到登陆界面
//			redirect(base_url().'/adminHome/loginError','location');
//			return ;
//
//			
//			//if 登陆成功
//			$this->getUsers();
//		}


		function getUsers(){
					$whichGrid = 1;
					if($whichGrid == 0){	
//					$table="orderdetail";
//					$cols= array();
//					$startRowIndex=0;
//					$endRowIndex=0;
//					
//					$this->load->model('user/getUserModel');
//					$result=$this->getUserModel->getRowsModel($table, $cols,$startRowIndex,$endRowIndex);
//					$data['query'] = $result['query'];
//					$count = $result['count'];
//					 
//					$this->load->view('user/viewUserList_DIV',$data);
					}
					
					
					//--- 使用Flexigrid
					if ($whichGrid == 1){
					$this->load->view('user/viewUserList');
					}

 		
		}
		
 
		// 获取某个table中所有的列名,以初始化Flexigrid的列名
		public function getAllColName(){
			$table = $_POST['table'];
			
			$fields = $this->db->list_fields($table);

			echo json_encode($fields);
			
		}
		
		// 此处接受来自客户端的要删除的id号数组, 此功能目前处于冻结状态,因为删除功能是由Flexigrid自身实现的
		public function delRows()
		{
			///
			
			$items = rtrim($_POST['items'],",");
			$ids=explode(",",$items);
			$table = $_POST['tableName'];
			$sql = "DELETE FROM ".$table." WHERE id IN ($items)";
			
			$total = count(explode(",",$items)); 
			 
			$result = $this->runSQL($sql);
	 		//$this->db->where('id', $ids);
			//$data=$this->db->delete('orderdetail'); 
 

			$total = $this->db->affected_rows(); 


			/// Line 18/19 commented for demo purposes. The MySQL query is not executed in this case. When line 18 and 19 are uncommented, the MySQL query will be executed. 
			header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
			header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
			header("Cache-Control: no-cache, must-revalidate" );
			header("Pragma: no-cache" );
			header("Content-type: text/x-json");
			$json = "";
			$json .= "{\n";
			$json .= "query: '".$sql."',\n";
			$json .= "total: $total,\n";
			$json .= "}\n";
			echo $json;
			
		}
	
	// 修改订单状态
	public function changeOrderStatus(){
		
		$items = rtrim($_POST['items'],",");
		$ids=explode(",",$items);
		$table = $_POST['tableName'];
		$status = $_POST['status'];
		$sql = "UPDATE $table SET status='".$status."' WHERE id IN ($items)";
		
		$total = count(explode(",",$items)); 
		
//		$file=fopen( "c:/phptest.txt","w");
//		fwrite($file,json_encode($sql));
//		fclose($file);
		$result = $this->db->query($sql) or die("error"); 
		
		echo json_encode("1");
	}
		
	// 从某个table中获取记录,返回给Flexigrid
	public function readDataFromDB(){

		$page = $_POST['page'];
		$rp = $_POST['rp'];
		$sortname = $_POST['sortname'];
		$sortorder = $_POST['sortorder'];
		$table = $_POST['sqlTable'];
		$tStart = $_POST['tStart'];
		$tEnd = $_POST['tEnd'];
		$timeFlag = $_POST['timeFlag'];
        $orderStatusFlag =  $_POST['orderStatusFlag'];
        $wholeWordOrNot = $_POST['wholeWordOrNot'];

		if (!$sortname) $sortname = 'id';
		if (!$sortorder) $sortorder = 'desc';
		// 关键字搜索
		if($_POST['query']!=''){
			$where = "WHERE `".$_POST['qtype']."` LIKE '";
			if(!$wholeWordOrNot)  $where .= "%";
			$where .= $_POST['query'];
			if(!$wholeWordOrNot) $where .= "%";
			$where .= "' ";
		} else {
			$where = "";
		}
		
		// 时间筛选
		$whereTime="";
		if(!$where) $whereTime = "WHERE ctime>='".$tStart."' and ctime<='".$tEnd."'";
		else 		$whereTime .= " and ctime>='".$tStart."' and ctime<='".$tEnd."'";
		if(!$timeFlag) $whereTime="";
		$where .= $whereTime;
		
		// 订单状态筛选
		$whereOrder="";
		if($_POST['orderStatus']!=''){			
			if(!$where) $whereOrder = "WHERE status='".$_POST['orderStatus']."'";
			else $whereOrder = " and status='".$_POST['orderStatus']."'";
			if(!$orderStatusFlag) $whereOrder = "";
			$where .= $whereOrder;
		}
	 
		$sort = "ORDER BY $sortname $sortorder";

		
		if (!$page) $page = 1;
		if (!$rp) $rp = 10;
		
		$start = (($page-1) * $rp);
		
		$limit = "LIMIT $start, $rp";
		
 		$sql = "SELECT * FROM $table $where $sort $limit";

		$result = $this->runSQL($sql);

		$total = $this->countRec('*',$table, $where);
		
		$col = $result->list_fields($table);
		$colCount = count($col);


		//-----返回json数据
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
		header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
		header("Cache-Control: no-cache, must-revalidate" );
		header("Pragma: no-cache" );
		header("Content-type: text/x-json");
		$json = "";
		$json .= "{\n";
		$json .= "page: $page,\n";
		$json .= "total: $total,\n";
		$json .= "rows: [";
		$rc = false;

		
		foreach ($result->result_array() as $row){
			if ($rc) $json .= ",";
			$json .= "\n{";
			$json .= "id:'".$row[$col[0]]."',";
			$json .= "cell:['";
			$json .= $row[$col[0]]."','".$row[$col[0]];
			
			for ($ic = 1; $ic < $colCount; $ic++){
				$json .= "','";
				$json .= addslashes($row[$col[$ic]]);

			}
			$json .= "']";
			$json .= "}";
			$rc = true;

		}
		$json .= "]\n";
		$json .= "}";
		
		echo $json;
		}// readDataFromDB
		
		
		//-执行一个完整的SQL语句
		function runSQL($rsql) {
	 		$result = $this->db->query($rsql) or die("error"); 
			return $result;
		}
		
		// 统计满足条件的总数量, 用于表格分页计数
		function countRec($fname,$tname,$where) {
			$sql = "SELECT COUNT($fname) FROM $tname $where";
			$result = $this->db->query($sql) or die("error"); 
			$count = $result->row_array();
			return $count["COUNT($fname)"];

		}
		
		
	}
?>
 





















